package com.hhss.qishi.mapper.provider;

import com.hhss.qishi.entity.Article;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

/**
 * @Author :Administrator
 * @Description
 * @Date :2019-05-27 15:13.
 */
public class ArticleProvider {

	private static String SQL_ENTITY = "id, uuid,"
			+ " title, summary, content, hits, category_id AS categoryId,"
			+ " article_classification AS articleClassification, article_status AS articleStatus,"
			+ " article_type AS articleType, is_public AS isPublic, is_topping AS isTopping,"
			+ " status, create_user AS createUser, create_time AS createTime, last_update_user AS lastUpdateUser,"
			+ " last_update_time AS lastUpdateTime, create_user AS createUser, last_update_user AS lastUpdateUser";

	private static String TABLE_NAME = "tb_article";

	/**
	 * 查找文章集合
	 * @param article
	 * @return
	 */
	public String getListSQL(final Article article) {
		return new SQL(){{
			SELECT(SQL_ENTITY);
			FROM(TABLE_NAME);
			WHERE("1=1");
			if(!StringUtils.isEmpty(article.getTitle())) {
				AND().WHERE("title like CONCAT('%',#{article.title},'%')");
			}
			if(!StringUtils.isEmpty(article.getCategoryId())) {
				AND().WHERE("category_id = #{article.categoryId}");
			}
			if(!StringUtils.isEmpty(article.getIsPublic())) {
				AND().WHERE("is_public = #{article.isPublic}");
			}
			if(!StringUtils.isEmpty(article.getArticleStatus())) {
				AND().WHERE("article_status = #{article.articleStatus}");
			}
		}}.toString();
	}
}
